//index.js
//获取应用实例
var app = getApp();
var md5 = require("../../utils/md5.js");
var starscore = require("../../templates/starscore/starscore.js");

Page({
  data: {
    indicatorDots: true,
    autoplay: true,
    interval: 3000,
    duration: 1000,
    loadingHidden: false, // loading
    userInfo: {},
    width: wx.getStorageSync('width'),
    height: wx.getStorageSync('height'),
    swiperCurrent: 0,
    selectCurrent: 0,
    categories: [],
    activeCategoryId: 0,
    goods: [],
    scrollTop: "0",
    loadingMoreHidden: true,
    banners: '', 
    hasNoCoupons: true,
    coupons: []
  },
  tabClick: function (e) {
    this.setData({
      activeCategoryId: e.currentTarget.id
    });
    this.getGoodsList(this.data.activeCategoryId);
  },
  //事件处理函数
  swiperchange: function (e) {
    //console.log(e.detail.current)
    this.setData({
      swiperCurrent: e.detail.current
    })
  },
  toDetailsTap: function (e) {
    wx.navigateTo({
      url: "/pages/goods-details/index?id=" + e.currentTarget.dataset.id
    })
  },
  tapBanner: function (e) {
    if (e.currentTarget.dataset.id != 0) {
      wx.navigateTo({
        url: "/pages/goods-details/index?id=" + e.currentTarget.dataset.id
      })
    }
  },
  bindTypeTap: function (e) {
    this.setData({
      selectCurrent: e.index
    })
  },
  scroll: function (e) {
    //  console.log(e) ;
    var that = this, scrollTop = that.data.scrollTop;
    that.setData({
      scrollTop: e.detail.scrollTop
    })
    // console.log('e.detail.scrollTop:'+e.detail.scrollTop) ;
    // console.log('scrollTop:'+scrollTop)
  },
  onLoad: function (options) {
    var that = this
    if (options.shop_id != undefined) {//检测一下当前shop_id是都存在，如果存在取出更新
      app.globalData.shopId = options.shop_id;
    }
    that.getShopProduct();
    that.getBanners();
    that.getInfo();
    that.setData({
      open_id: app.globalData.openId,
      user_id: app.globalData.userId,
    })
  },
  getShopProduct: function () {
    var that = this
    var service = 'Shop.GetShopInfo';
    var openid = app.globalData.openId;
    var user_id = app.globalData.userId;
    var shop_id = app.globalData.shopId;
    var sign = md5.md5(openid + service + shop_id + user_id);
    wx.request({
      url: app.globalData.serviceUrl + service,
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        user_id: user_id,
        openid: openid,
        shop_id: shop_id,
      },
      success: (res) => {
        var result = res.data.data;
        if (result.code == 0) {
          console.log(result.info.shop_product)
          this.setData({
            goods: result.info.shop_product,
          })
        } else {
          wx.showToast({
            title: '获取失败',
            icon: 'success',
            duration: 2000
          })
        }
      },
    })
  },

//商铺图片
  getBanners:function(){
    var that = this
    var service = 'Shop.GetShopPicture';
    var openid = app.globalData.openId;
    var user_id = app.globalData.userId;
    var shop_id = app.globalData.shopId;
    var sign = md5.md5(openid + service + shop_id + user_id);
    wx.request({
      url: app.globalData.serviceUrl + service,
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        user_id: user_id,
        openid: openid,
        shop_id: shop_id,
      },
      success: (res) => {
        var result = res.data.data;
        console.log(result.info);
        if (result.code == 0) {
          that.setData({
            banners: result.info
          });
        }else{
          that.setData({
            banners: [{ picture_url: "https://hongbao.zntech-china.com/attachment/images/wxcx/new_shop.jpg" }]
          });
          if(shop_id==1067){
            var now = new Date().getTime();
            var time1 = new Date('2018-08-01 00:00:00').getTime();
            var time2 = new Date('2018-08-05 23:59:59').getTime();
            if (now > time1 && now < time2){
              var banner = [{ picture_url: "https://hongbao.zntech-china.com/attachment/images/wxcx/new_shop.jpg" }, { picture_url: "https://hongbao.zntech-china.com/attachment/images/wxcx/taimang_huodong.jpg" }];
              that.setData({
                banners: banner
              });
            }
          }
        }
      }
    })
  },
  
  //商铺公告
  getInfo:function(){
    var that = this
    var service = 'Shop.GetShopInformation';
    var openid = app.globalData.openId;
    var user_id = app.globalData.userId;
    var shop_id = app.globalData.shopId;
    var sign = md5.md5(openid + service + shop_id + user_id);
    wx.request({
      url: app.globalData.serviceUrl + service,
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        user_id: user_id,
        openid: openid,
        shop_id: shop_id,
      },
      success: (res) => {
        var result = res.data.data;
        if (result.code == 0) {
          this.setData({
            info: result.info
          })
        } else {
          this.setData({
            info: [{ information_title: "我的店铺上线了" }]
          })
          if (shop_id == 1067) {
            var now = new Date().getTime();
            var time1 = new Date('2018-08-01 00:00:00').getTime();
            var time2 = new Date('2018-08-05 23:59:59').getTime();
            if (now > time1 && now < time2) {
              var info = [{ information_title: "我的店铺上线了" }, { information_title: "买任一款饮品送泰芒经典杯！" }];
              this.setData({
                info: info
              });
            }
          }
        }
      },
    })
  },

  toDetailsTap: function (e) {
    wx.navigateTo({
      url: "/pages/goods-details/index?id=" + e.currentTarget.dataset.id
    })
  },
  
  //分享小程序
  onShareAppMessage: function () {
    return {
      title: app.globalData.shopName,
      path: '/pages/start/start?shop_id=' + app.globalData.shopId,
      imageUrl: app.globalData.shopPic,
    }
  },
})
